package com.dtstack.rocketmq.utils;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

/**
 * DateUtil
 *
 * <p>
 * </p>
 *
 * @author chuanbai
 * @date 2024/11/5
 */
public class DateUtil {

    /**
     *  获取当前utc时间
     *  其格式化为 ISO 8601 的字符串
     */
    public static String getUTC(){
        return DateTimeFormatter
                .ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'")
                .withZone(ZoneOffset.UTC)
                .format(Instant.now());
    }

    /**
     * 获取当前UTC时间 前推指定小时
     */
    public static String getUTCBeforeHours(int hours){
        return DateTimeFormatter
                .ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'")
                .withZone(ZoneOffset.UTC)
                .format(Instant.now().minus(Duration.ofHours(hours)));
    }

    /**
     * 获取当前UTC时间 后推指定小时
     */
    public static String getUTCAfterHours(int hours){
        return DateTimeFormatter
                .ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'")
                .withZone(ZoneOffset.UTC)
                .format(Instant.now().plus(Duration.ofHours(hours)));
    }


    public static void main(String[] args) {
        System.out.println(LocalDateTime.now());
        System.out.println(getUTC());
        System.out.println(getUTCAfterHours(48));
    }
}
